import time

import pyautogui
import pygetwindow as gw

from my_package.button_utils import find_menu_btn, find_memorandum_btn, find_cancel_btn, cancel_btn_img_path
from my_package.xqol_utils import resource_path, find_btn_location_window, get_game_windows, get_game_window

# 默认场景判定逻辑：（截取左中的菜单按钮位置，如果能找到，就视为是刚登录时的场景，没有打开其它任何界面！）



# 主菜单场景（点击了菜单之后的界面）
mina_menu_img_path = resource_path("images/screen_img/主菜单.png")
# 以及其它场景


# 背包场景（打开了背包界面）
back_pack_img_path = resource_path("images/screen_img/背包.png")
# 背包场景之存入仓库
deposit_img_path = resource_path("images/screen_img/存入仓库.png")

# 备忘场景
memo_img_path = resource_path("images/screen_img/备忘.png")

# 聊天场景
chat_img_path = resource_path("images/screen_img/聊天记录.png")
# 频道发言
speak_img_path = resource_path("images/screen_img/频道发言.png")

# 周围列表
surrounding_list_img_path = resource_path("images/screen_img/周围列表.png")

# 邮件
email_img_path = resource_path("images/screen_img/邮件.png")

# 队伍成员
team_members_img_path = resource_path("images/screen_img/队伍成员.png")

# 回合（即战斗场景）
battle_img_path = resource_path("images/screen_img/回合.png")
# 自动按钮
auto_btn_img_path = resource_path("images/ring_pk_img/auto_btn.png")
# 比武结束场景
battle_end_path = resource_path("images/screen_img/比武结束.png")

# 任务列表
task_list_img_path = resource_path("images/screen_img/任务列表.png")

# 添加入集合
screen_img_list = [mina_menu_img_path, back_pack_img_path, memo_img_path, chat_img_path, speak_img_path,
                   surrounding_list_img_path, email_img_path, team_members_img_path, battle_img_path,
battle_end_path
                   ]

def is_default_screen(window=None):
    """
    判断是否处于默认场景（窗口界面中，同时出现菜单按钮+备忘按钮）
    :param window:
    :return:
    """
    if window is None:
        print("无法找到窗口，退出程序")
        exit()
    #print(window.title, "判断当前场景")
    time.sleep(0.5)
    # 找到菜单按钮区域，
    res_menu = find_menu_btn(window, False)
    # 以及备忘按钮区域
    res_memo = find_memorandum_btn(window, False)
    # 判断两者是否都有，如果都有，则返回True
    if res_menu and res_memo > 0:
        print(window.title, "当前处于 默认场景 ")
        return True
    # 否则返回False
    return False


def is_ring_pk_battle_scene(window=None):
    """
    判断是否在战斗场景
    :param window:
    :return:
    """
    res_auto = find_btn_location_window(window, auto_btn_img_path)
    if len(res_auto) > 0:
        return True
    return False


def get_screen_name(window=None):
    """
    判断是否哪个场景（获取窗口所在场景名称）
    :param window: 窗口
    :return: 场景的名称，具体可查看对应的图片
    """
    if window is None:
        return "没有找到窗口，可能根本就没有启动窗口"
    if is_default_screen(window):
        return "默认场景"
    for screen_img in screen_img_list:
        try:
            window.activate()
        except Exception as e:
            print(window.title, "窗口激活", e)
        res = find_btn_location_window(window, screen_img)
        if len(res) > 0:
            # 获取场景名称，例如："主菜单"
            scene_name = get_file_base_name(screen_img)
            print(f"匹配到场景：{scene_name}")
            return scene_name
    return None


def get_file_base_name(file_path):
    """
    从文件路径中提取最后一个斜杠后的文件名（去除扩展名）

    :param file_path: 文件路径字符串
    :return: 不带扩展名的文件名
    """
    import os
    return os.path.splitext(os.path.basename(file_path))[0]


def return_default_screen(window=None, screen_name=None):
    """
    回到默认主场景
    :param window: 窗口
    :param screen_name: 场景名称
    :return:
    """
    if window is None:
        return
    if is_default_screen(window):
        print("当前场景已经是默认场景")
        return True
    if screen_name is None:
        screen_name = get_screen_name(window)
    print(window.title, "处于", screen_name, "场景")
    res_cancel = find_btn_location_window(window, cancel_btn_img_path)
    if screen_name == "主菜单":
        print("当前场景是主菜单，点击一次取消按钮，即可返回默认场景")
        if len(res_cancel) > 0:
            pyautogui.click(window.left + res_cancel[0] + 3, window.top + res_cancel[1] + 3)
    elif screen_name == "比武结束":
        time.sleep(0.2)
        pyautogui.click(window.left + res_cancel[0] + 3, window.top + res_cancel[1] + 3)
    elif screen_name == "背包":
        print("当前场景是背包", res_cancel)
        if len(res_cancel) == 0:
            return False
        # 背包场景，又可能包含其它各种场景
        # 如果找到了[存入仓库],则需要点击2次取消
        if find_btn_location_window(window, deposit_img_path):
            print("找到[存入仓库]按钮，点击2次取消")
            pyautogui.click(window.left + res_cancel[0] + 3, window.top + res_cancel[1] + 3)
            pyautogui.click(window.left + res_cancel[0] + 3, window.top + res_cancel[1] + 3)
        else:
            print("没有找到[存入仓库]按钮，点击一次取消")
            pyautogui.click(window.left + res_cancel[0] + 3, window.top + res_cancel[1] + 3)
    elif screen_name == "备忘":
        print("当前场景是备忘")
        pyautogui.click(window.left + res_cancel[0] + 3, window.top + res_cancel[1] + 3)
    elif screen_name == "聊天记录":
        print("当前场景是聊天记录")
    elif screen_name == "频道发言":
        print("当前场景是频道发言")
    elif screen_name == "周围列表":
        print()
    elif screen_name == "邮件":
        print("当前场景是邮件")
    elif screen_name == "队伍成员":
        print("当前场景是队伍成员")
    elif screen_name == "任务列表":
        print("当前场景是任务列表")
    elif screen_name == "回合" or screen_name == "战斗":
        print("当前场景是战斗场景")
        while True:
            print("当前场景是战斗场景")
            time.sleep(1)
            # 找比武结束场景或者默认场景
            flag = is_ring_pk_battle_scene(window)
            if not flag:
                print("当前场景不是战斗场景，返回默认场景")
                break
        # 点击取消按钮
        if len(res_cancel) > 0:
            pyautogui.click(window.left + res_cancel[0] + 3, window.top + res_cancel[1] + 3)
    else:
        print(window.title, "当前场景未知，请继续完善调试！！！")



def main():
    window = get_game_window("xqolhm27")
    while True:
        screen_name = get_screen_name(window)
        print("当前场景", screen_name)

if __name__ == '__main__':
    main()